package com.youren.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youren.health.dao.SetmealDao;
import com.youren.health.entity.PageResult;
import com.youren.health.entity.QueryPageBean;
import com.youren.health.exception.HealthException;
import com.youren.health.pojo.Setmeal;
import com.youren.health.service.SetmealService;
import com.youren.health.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-24 16:57
 **/
@Service( interfaceClass = SetmealService.class)
public class SetmealServiceImpl
        implements SetmealService{
    @Autowired
    private SetmealDao setmealDao;
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
//        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
//            queryString="%"+queryString+"%";
//        }
        Page<Setmeal> page = setmealDao.findByPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public Integer add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        Integer setmealId = setmeal.getId();
        if (checkgroupIds!=null) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.addsetmealIdcheckgroupId(setmealId,checkgroupId);
            }
        }
        return setmealId;
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = setmealDao.findById(id);
        return setmeal;
    }

    @Override
    public List<Integer> findCheckGroupIdList(Integer id) {
        List<Integer> checkGroupIdList = setmealDao.findCheckGroupIdList(id);
        return checkGroupIdList;
    }

    @Override
    @Transactional
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.update(setmeal);
        Integer id = setmeal.getId();
        setmealDao.deleteCheckGroupIdBySetmealId(id);
        if (checkgroupIds!=null) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.addsetmealIdcheckgroupId(id,checkgroupId);
            }
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer id) throws HealthException{
        long count = setmealDao.findCountById(id);
        if (count>0) {
            throw new HealthException("有预约未到诊，无法删除该套餐！");
        }
        setmealDao.deleteCheckGroupIdBySetmealId(id);
        setmealDao.deleteById(id);
    }

    @Override
    public List<String> findImgList() {
        return setmealDao.findImgList();
    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> setmealList = setmealDao.findAll();
        setmealList.forEach(setmeal -> {
            setmeal.setImg(OSSUtil.DOMAIN+setmeal.getImg());
        });
        return setmealList;
    }
    @Override
    public Setmeal findDetailById(Integer id) {
        Setmeal setmeal = setmealDao.findDetailById(id);
        setmeal.setImg(OSSUtil.DOMAIN+setmeal.getImg());
        return setmeal;
    }
}
